/*
 *   Servlet for surfacing syndicated content
 *   Copyright (C) 2011 Glenn Engstrand
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.dynamicalsoftware.news.publisher;

import com.dynamicalsoftware.news.NewsPublisher;

import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.Properties;
import java.util.List;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.simpledb.AmazonSimpleDB;
import com.amazonaws.services.simpledb.AmazonSimpleDBClient;
import com.amazonaws.services.simpledb.model.CreateDomainRequest;
import com.amazonaws.services.simpledb.model.PutAttributesRequest;
import com.amazonaws.services.simpledb.model.ReplaceableAttribute;

/**
 * publishing displayed references to Amazon Simple DB
 * @author glenn
 *
 */
public class SimpleDBPublisher implements NewsPublisher {

	private static final String domain = "news";
	private static final Logger log = Logger.getLogger(SimpleDBPublisher.class.getCanonicalName());
	
	private AmazonSimpleDB db = null;
	private String secretKey = null;
	private String accessKey = null;
	
	/**
	 * authenticate and connect to the data store
	 * @return connection to db
	 * @throws IOException
	 */
	private AmazonSimpleDB getConnection() throws IOException {
		if (db == null) {
			Properties p = new Properties();
			p.setProperty("secretKey", getSecretKey());
			p.setProperty("accessKey", getAccessKey());
			ByteArrayOutputStream pos = new ByteArrayOutputStream();
			p.store(pos, "credentials");
			ByteArrayInputStream pis = new ByteArrayInputStream(pos.toByteArray());
			PropertiesCredentials auth = new PropertiesCredentials(pis);
			db = new AmazonSimpleDBClient(auth);
			db.createDomain(new CreateDomainRequest(domain));
		}
		return db;
	}
	
	public void publish(String key, String title, String link, String description) {
		List<ReplaceableAttribute> req = new ArrayList<ReplaceableAttribute>();
		if (link != null) {
			req.add(new ReplaceableAttribute("link", link, false));
		}
		if (title != null) {
			req.add(new ReplaceableAttribute("title", title, false));
		}
		if (description != null) {
			req.add(new ReplaceableAttribute("description", description, false));
		}
		try {
			getConnection().putAttributes(new PutAttributesRequest(domain, key, req));
		} catch (AmazonServiceException e) {
			log.log(Level.SEVERE, "Service Exception.\n", e);
		} catch (AmazonClientException e) {
			log.log(Level.SEVERE, "Client Exception.\n", e);
		} catch (IOException e) {
			log.log(Level.SEVERE, "Cannot get input.\n", e);
		}
	}

	public void close() {
	}

	public String getSecretKey() {
		return secretKey;
	}

	public void setSecretKey(String secretKey) {
		this.secretKey = secretKey;
	}

	public String getAccessKey() {
		return accessKey;
	}

	public void setAccessKey(String accessKey) {
		this.accessKey = accessKey;
	}
}
